package com.jy.wms.service;

import com.jy.wms.common.base.BaseService;
import com.jy.wms.dao.WmsProvinceRankingReportMapper;
import com.jy.wms.pojo.DirectSellingProvincesAndCities;
import com.jy.wms.pojo.WmsProvinceRankingReport;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Date;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;

//报表导出
@Service
public class WmsSupportNumberReportExportService extends BaseService<WmsProvinceRankingReportMapper, WmsProvinceRankingReport> {

    /**
     * 直销省市排名报表导出（根据时间查询订单数量）
     * @return List<WmsProvinceRankingReport1>
     */
    public List<WmsProvinceRankingReport> baseselectbathprimarykeys(DirectSellingProvincesAndCities ceshi) {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(ceshi.getFpsj_start()!=null){
            java.sql.Date date =new java.sql.Date(new Long(ceshi.getFpsj_start()));
            ceshi.setFpsj_start(sdf.format(date));
        }
        if(ceshi.getFpsj_end()!=null){
            java.sql.Date date=new Date(new Long(ceshi.getFpsj_end()));
            ceshi.setFpsj_end(sdf.format(date));
        }

        List<WmsProvinceRankingReport> Sales= dao.baseselectbyprimaryExport(ceshi);

        int i = 0;//记录排名
        int sddl = 0;//记录订单量
        double Ddslbfb = 0;
        double zddbfb = 0;
        int  ddl=0;
        int xse=0;
        for (WmsProvinceRankingReport user : Sales) {
            System.out.println(user.getSheng()+"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"+user.getXse());
            //订单量排序
            if (sddl == 0) {
                sddl = Integer.parseInt(user.getDdl());
                i++;
            } else {
                if (Integer.parseInt(user.getDdl()) == sddl) {
                } else {
                    sddl = Integer.parseInt(user.getDdl());
                    i++;
                }
            }
            user.setDdlpm(i);

            //计算所有省的订单量
            int ddl1= Integer.parseInt(user.getDdl());
            ddl+=ddl1;

            //计算所有省的销售额
            if (!user.getXse().equals("") && !user.equals(null)){
                xse+=user.getXse();
            }else if (!user.getXse().equals("") && !user.equals(null)){
                Double xse1= Double.parseDouble("0.0");
                xse+=xse1;
            }
        }


        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        //计算百分比
        for (WmsProvinceRankingReport user : Sales) {
            //订单量百分比
            int dd= Integer.parseInt(user.getDdl());
            if(ddl==0){
                user.setDdlbfb(0+"%");
            }else{
                String ddbfb = numberFormat.format((float) dd / (float) ddl * 100);
                user.setDdlbfb(ddbfb+"%");
            }
            //销售额百分比
            if (xse==0.0){
                user.setXsezb(0.00+"%");
            }else{
                BigDecimal b = new BigDecimal(user.getXse() / xse * 100);
                Double xx = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                user.setXsezb(xx+"%");
            }
        }
            return Sales;
    }
}
